﻿using System;
using System.Collections.Generic;
using System.Configuration;

namespace Landscape
{
    class Configuration : ConfigurationSection
    {
        #region "Sub section definitions"
        [ConfigurationProperty("landscapeSettings")]
        protected SettingCollection Settings
        {
            get { return (SettingCollection)this["landscapeSettings"]; }
            set { this["landscapeSettings"] = value; }
        }

        [ConfigurationProperty("landscapeSources")]
        protected SourceCollection Sources
        {
            get { return (SourceCollection)this["landscapeSources"]; }
            set { this["landscapeSources"] = value; }
        }

        [ConfigurationProperty("landscapeIncludes")]
        protected SourceCollection Includes
        {
            get { return (SourceCollection)this["landscapeIncludes"]; }
            set { this["landscapeIncludes"] = value; }
        }
        [ConfigurationProperty("landscapeWidgets")]
        protected SourceCollection Widgets
        {
            get { return (SourceCollection)this["landscapeWidgets"]; }
            set { this["landscapeWidgets"] = value; }
        }
        #endregion

        #region "Publicly accessible stuff"

        public List<KeyValuePair<int, string>> SourceDirs
        {
            get
            {
                List<KeyValuePair<int, string>> result = new List<KeyValuePair<int, string>>();
                foreach (SourceElement s in Sources)
                {
                    result.Add(new KeyValuePair<int, string>(s.Priority, Globals.AbsPath(s.Path)));
                }
                return result;
            }
        }

        public List<KeyValuePair<int, string>> IncludeDirs
        {
            get
            {
                List<KeyValuePair<int, string>> result = new List<KeyValuePair<int, string>>();
                foreach (SourceElement s in Includes)
                {
                    result.Add(new KeyValuePair<int, string>(s.Priority, Globals.AbsPath(s.Path)));
                }
                return result;
            }
        }

        public List<KeyValuePair<int, string>> WidgetDirs
        {
            get
            {
                List<KeyValuePair<int, string>> result = new List<KeyValuePair<int, string>>();
                foreach (SourceElement s in Widgets)
                {
                    result.Add(new KeyValuePair<int, string>(s.Priority, Globals.AbsPath(s.Path)));
                }
                return result;
            }
        }

        [ConfigurationProperty("landscapeXsltExtensions")]
        public XsltExtensionCollection XsltExtensions
        {
            get { return (XsltExtensionCollection)this["landscapeXsltExtensions"]; }
            set { this["landscapeXsltExtensions"] = value; }
        }

        public string Setting(string settingName) { 
            string result = "";
            if(this.Settings.Count > 0){
                foreach (SettingElement s in this.Settings) { 
                    if(s.Key == settingName && s.Value != string.Empty){
                        result = s.Value;
                        break;
                    }
                }
            }

            return result;
        }
        #endregion
    }

    #region "Settings"
    class SettingCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new SettingElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((SettingElement)element).Key;
        }
    }

    class SettingElement : ConfigurationElement
    {
        [ConfigurationProperty("key")]
        public string Key
        {
            get { return (string)this["key"]; }
            set { this["key"] = value; }
        }

        [ConfigurationProperty("value")]
        public string Value
        {
            get { return (string)this["value"]; }
            set { this["value"] = value; }
        }
    }
    #endregion

    #region "Source paths"
    class SourceCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new SourceElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((SourceElement)element).Path;
        }
    }

    class SourceElement : ConfigurationElement
    {
        [ConfigurationProperty("priority")]
        public int Priority
        {
            get { return (int)this["priority"]; }
            set { this["priority"] = value; }
        }

        [ConfigurationProperty("path")]
        public string Path
        {
            get { return (string)this["path"]; }
            set { this["path"] = value; }
        }
    }
    #endregion

    #region Xslt Extension Configuration
    class XsltExtensionCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new XsltExtensionElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((XsltExtensionElement)element).Type;
        }
    }

    class XsltExtensionElement : ConfigurationElement
    {
        [ConfigurationProperty("assembly")]
        public string Assembly
        {
            get { return (string)this["assembly"]; }
            set { this["assembly"] = value; }
        }

        [ConfigurationProperty("type")]
        public string Type
        {
            get { return (string)this["type"]; }
            set { this["type"] = value; }
        }

        [ConfigurationProperty("alias")]
        public string Alias
        {
            get { return (string)this["alias"]; }
            set { this["alias"] = value; }
        }
    }
    #endregion
}
